<?php

namespace app\admin\controller;

use app\admin\model\Baners;
use app\common\BaseHelper as Helper;
use app\common\Common;
use app\admin\model\Image;

use think\Request;

/**
 * 轮播图
 */
class Adbanner extends Base
{

    public function _initialize()
    {
        parent::_initialize();
        $defalutwhere['status'] = 1;
        $this->defaultWhere = $defalutwhere;

        $this->redirect = 'admin/adbanner/index';
    }

    public function index(){
        $request = $this->request;
        $param = $request->param();
        // Reset filter
        if ($request->param('reset')) {
            return redirect(fullUrl($request->path()));
        }
        if($request->isAjax()){
            $model = model($this->model);

            // 每页起始条数
            $start = $param['start'];
            // 每页显示条数
            $length = $param['length'];
            // 排序条件
            $columns = $param['order'][0]['column'];
            $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];
            $where = $this->getFilterWhere($request);
            if($this->defaultWhere){
                //$model = $model->where($this->defaultWhere);
                $where = array_merge($this->defaultWhere, $where);
            }
            /*if($this->defaultOrder){
                $model = $model->order($this->defaultOrder);
            }*/
            $where['site'] = ['in',[1,2]];
            $list = $model->where($where)->limit($start, $length)->order($order)->select();
            $count = $model->where($where)->count();
            foreach($list as $v){
                if ($v['site'] ==1){
                    $v['site'] = '首页';
                }
                if ($v['site'] ==2){
                    $v['site'] = '建党要闻';
                }
            }
            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count,
            ];

            return json($result);
        }

        return self::show($this->indexView, [
            'pageSize' => ['url' => fullUrl($request->path())],
            'searchFields' => $this->searchFields,
            'param' => $request->param()
        ]);
    }
    private function show($template, $data = []) {

        return $this->view->fetch($template, $data);
    }
    /**
     * Add
     */
    public function add(Request $request){
        // 链接类型
        $linkTypes = $this->get_link_type();

        $this->assign('linkTypes', $linkTypes);

        return view();
    }

    function get_link_type()
    {
        return [
            '1' => '首页',
            '2' => '建党要闻',
        ];
    }

    /**
     * Add Post
     */
    public function addPost(Request $request, $redirect = ''){
        $model = model($this->model);

        //save data
        if ($request->isPost()) {
            $data = $request->param();
            // Insert data

            $data['createDate'] = date('Y-m-d H:i:s',time());
            $data['status'] = 1;
            $data['sort'] = $data['sorting'];
            $data['link_type'] = $data['type'];

            $data['createOper'] = $this->admin->id;

            $result = $model->save($data);

            if($result !== false) {
                // 单图
                if(isset($data['imgId']) && !empty($data['imgId'])){
                    Image::where('id', $data['imgId'])->update(['relatedId' => $data['id'], 'relatedTable' => 'Adbanner']);
                }
                // 详情图
                if(isset($data['detailImgIds']) && !empty($data['detailImgIds'])){
                    foreach ($data['detailImgIds'] as $detailImgId) {
                        Image::where('id', $detailImgId)->update(['relatedId' => $data['id'], 'relatedTable' => 'Adbanner']);
                    }
                }
                if ($redirect) {
                    return $this->success('添加成功！', $redirect);
                } else {
                    return $this->success('添加成功！', 'admin/' . strtolower($this->model) . '/index');
                }
            } else {
                return $this->error($model->getError());
            }
        } else {
            return $this->error('');
        }
    }

    /**
     * Edit
     */
    public function edit(Request $request){
        $model = model($this->model);
        $id = $request->param('id');
        // Get data info
        $info = $model->find($id);

        if(!$info){
            return $this->error('error !', 'admin/' . strtolower($this->model) . '/index');
        }

        // 链接类型
        $linkTypes = $this->get_link_type();

        $this->assign('linkTypes', $linkTypes);
        $info['linktype'] = $info['link_type'];

        return $this->view->fetch('edit', [
            'info' => $info
        ]);
    }

    /**
     * Edit Post
     */
    public function editPost(Request $request, $redirect = ''){
        $model = model($this->model);

        //save data
        if ($request->isPost()) {
            $data = $request->param();
            // Update data
            $data['updateDate'] = date('Y-m-d H:i:s',time());
            $data['updateOper'] = $this->admin->id;
            $data['status'] = 1;
            $data['link_type'] = $data['type'];

            $result = $model->allowField(true)->save($data, ['id' => $data['id']]);

            if($result !== false) {
                // 单图
                if (isset($data['imgId']) && !empty($data['imgId'])) {
                    Image::where('id', $data['imgId'])->update(['relatedId' => $data['id'], 'relatedTable' => 'Adbanner']);
                }
                // 详情图
                if (isset($data['detailImgIds']) && !empty($data['detailImgIds'])) {
                    foreach ($data['detailImgIds'] as $detailImgId) {
                        Image::where('id', $detailImgId)->update(['relatedId' => $data['id'], 'relatedTable' => 'Adbanner']);
                    }
                }

                if ($redirect) {
                    return $this->success('编辑成功！', $redirect);
                } else {
                    return $this->success('编辑成功！', 'admin/' . strtolower($this->model) . '/index');
                }
            } else {
                return $this->error($model->getError());
            }
        } else {
            return $this->error('error !');
        }
    }

    //删除
    public function delete(Request $request){
        if($request->isPost()){
            $param = $request->param();
            if(empty($param['id'])){
                return $this->error('参数错误');
            }
            $Baners = Baners::get($param['id']);
            if($Baners){
                if($Baners->status != 1){
                    return $this->error('轮播图已经删除');
                }
                $Baners->status = 2;
                if($Baners->save()){
                    return $this->success('删除成功',$this->redirect);
                }else{
                    return $this->error('删除失败');
                }
            }else{
                return $this->error('轮播图不存在');
            }
        }else{
            return $this->error('非法请求');
        }
    }


}